#include "video/video.h"
#include "core/engine.h"
#include "core/config.h"
#include "libs.h"

namespace video {
	namespace po = boost::program_options;

	Video::Video():
		core::System("Video", core::Task::SINGLETHREADED_REPEATING),
		mWindow(new Window("MainWindow"))
	{
		addSetting("Width", &mWindow->mWidth);
		addSetting("Height", &mWindow->mHeight);

		addSetting("RedBits", &mWindow->mRedBits);
		addSetting("GreenBits", &mWindow->mGreenBits);
		addSetting("BlueBits", &mWindow->mBlueBits);
		addSetting("AlphaBits", &mWindow->mAlphaBits);

		addSetting("DepthBits", &mWindow->mDepthBits);
		addSetting("StencilBits", &mWindow->mStencilBits);

		addSetting("Fullscreen", &mWindow->mFullscreen);
		addSetting("AutoSizeWindow", &mWindow->mAutoSizeWindow);
		addSetting("Title", &mWindow->mTitle);
	}

	Video::~Video() {
	}

	bool Video::init() {
		core::EventChannel chan;

		mWindow->create();

		chan.broadcast(WindowCreated(mWindow));

		if (!h3dInit()) {
			h3dutDumpMessages();
			return false;
		}

		h3dSetupViewport(0, 0, mWindow->mWidth, mWindow->mHeight, true);

		return true;
	}

	void Video::update() {
		glfwSwapBuffers();
	}

	void Video::shutdown() {
		h3dRelease();
		mWindow->destroy();
	}

	WindowCreated::WindowCreated(const boost::shared_ptr<Window>& win): 
		mWindow(win) 
	{
	}
}
